const connection = require("./mysql")
const multer = require('multer')
const path = require("path")

const userInfo = () => {
    let sql = `select * from users`
    return new Promise((resolve, reject) => {
        connection.query(sql, (err, res, field) => {
            if (err) reject(err)
            resolve(res)
        })
    })
}
// 注册
const register = (username, password) => {
    let sql = `INSERT INTO users (username, password, author)
    VALUES('${username}', '${password}', 0);`
    return new Promise((resolve, reject) => {
        connection.query(sql, (err, res, field) => {
            if (err) reject(err)
            resolve(res)
        })
    })
}
// 登录
const login = (username, password) => {
    let sql = `select * from users
    where username = '${username}' and password = '${password}';`
    return new Promise((resolve, reject) => {
        connection.query(sql, (err, res, field) => {
            if (err) reject(err)
            resolve(res)
        })
    })
}

// 用户功能

// 修改订单
const modifyOrderForm = (id, username, phone, date) => {
    const dateStart = date[0]
    const dateEnd = date[1]
    let sql = `UPDATE reserveHomestay
    SET dateStart = "${dateStart}", dateEnd = "${dateEnd}"
    WHERE id = "${id}" and username = "${username}" and phone = "${phone}";`
    return new Promise((resolve, reject) => {
        connection.query(sql, (err, res, field) => {
            if (err) reject(err)
            resolve(res)
        })
    })
}
// 退订名宿
const cancelHomestay = (id, username, phone) => {
    let sql = `DELETE FROM reserveHomestay WHERE id = ${id} and username = '${username}' and phone = '${phone}';`
    return new Promise((resolve, reject) => {
        connection.query(sql, (err, res, field) => {
            if (err) reject(err)
            resolve(res)
        })
    })
}
// 预订民宿
const reserveHomestay = (title, price, des, img, name, phone, date, username) => {
    const dateStart = date[0]
    const dateEnd = date[1]
    // console.log(username)
    // const { dateStart, dateEnd } = date
    let sql = `INSERT INTO reserveHomestay (title, price, des, img, name, phone, dateStart, dateEnd, username)
    VALUES('${title}', '${price}', '${des}', '${img}','${name}', '${phone}','${dateStart}', '${dateEnd}', '${username}');`
    return new Promise((resolve, reject) => {
        connection.query(sql, (err, res, field) => {
            if (err) reject(err)
            resolve(res)
        })
    })
}
// 查看所有订单
const checkAllOrderForm = (username) => {
    let sql = `SELECT * from reserveHomestay WHERE username = '${username}';`
    return new Promise((resolve, reject) => {
        connection.query(sql, (err, res, field) => {
            if (err) {
                reject(err)
            }
            resolve(res)
        })
    })
}
// 管理员功能

// 拿到民宿所有数据
const queryHomestay = () => {
    let sql = `SELECT * from homestayinfo;`
    return new Promise((resolve, reject) => {
        connection.query(sql, (err, res, field) => {
            if (err) reject(err)
            resolve(res)
        })
    })
}
// 增加民宿
const addHomestay = (title, price, des, img) => {
    let sql = `INSERT INTO homestayinfo (title, price, des, img)
    VALUES("${title}", "${price}", "${des}", "${img}");`
    return new Promise((resolve, reject) => {
        connection.query(sql, (err, res, field) => {
            if (err) reject(err)
            resolve(res)
        })
    })
}
// 修改民宿
const modifyHomestay = (id, title, price, des) => {
    let sql = `UPDATE homestayinfo
    SET title = "${title}", price = "${price}", des = "${des}"
    WHERE id = "${id}";`
    return new Promise((resolve, reject) => {
        connection.query(sql, (err, res, field) => {
            if (err) reject(err)
            resolve(res)
        })
    })
}
// 删除民宿
const deleteHomestay = (id) => {
    let sql = `DELETE FROM homestayinfo WHERE id = ${id};`
    return new Promise((resolve, reject) => {
        connection.query(sql, (err, res, field) => {
            if (err) reject(err)
            resolve(res)
        })
    })
}

// 上传图片
const storage = multer.diskStorage({
    destination: function (req, file, cb) {
        // 上传的文件目录（文件上传以后放在哪里）
        cb(null, './uploads')
    },
    filename: function (req, file, cb) {
        //上传的文件名字 （文件名+‘-’+时间戳+源文件的后缀名【例如：jpg/png......】）
        // 可以使用图片原来的名称，但是如果名称重复了，会被覆盖
        cb(null, file.fieldname + '-' + Date.now() + path.extname(file.originalname))
    }
})

module.exports = {
    userInfo,
    login,
    register,
    addHomestay,
    queryHomestay,
    deleteHomestay,
    modifyHomestay,
    uploadImage: multer({ storage: storage }),
    reserveHomestay,
    checkAllOrderForm,
    cancelHomestay,
    modifyOrderForm
}